package top.weiliang.springboot3.service.message.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.weiliang.springboot3.aop.AuthInfo;
import top.weiliang.springboot3.base.PageBean;
import top.weiliang.springboot3.base.R;
import top.weiliang.springboot3.dao.message.TbMessageMapper;
import top.weiliang.springboot3.entity.message.TbMessage;
import top.weiliang.springboot3.interceptor.AppInterceptor;
import top.weiliang.springboot3.service.auth.AuthService;
import top.weiliang.springboot3.service.message.MessageService;
import top.weiliang.springboot3.vo.PageVO;

@Service
@Slf4j
@RequiredArgsConstructor
public class MessageServiceImpl implements MessageService {
    private final TbMessageMapper tbMessageMapper;
    private final AuthService authService;

    @Override
    public R<String> insert(TbMessage tbMessage) {
        //获取当前登陆状态
        AuthInfo authInfo = AppInterceptor.getAuthInfo();
        //发帖人
        tbMessage.setUid(authInfo.getUid());
        int i = tbMessageMapper.insert(tbMessage);
        return i ==1 ? R.ok("发布成功", "") : R.fail("发布失败", "");
    }

    @Override
    public R<PageVO<TbMessage>> query(TbMessage tbMessage, PageBean pageBean) {
        AuthInfo authInfo = AppInterceptor.getAuthInfo();

        QueryWrapper<TbMessage> wrapper =  new QueryWrapper<>();
        wrapper.select("id","uid","title","hits","reply_count","likes","created");
        wrapper.like(StringUtils.hasText(tbMessage.getTitle()),
                "title", tbMessage.getTitle());
        wrapper.like(StringUtils.hasText(tbMessage.getContent()),
                "content", tbMessage.getContent());
        wrapper.orderByDesc("id");
        IPage<TbMessage> page = new Page<>(pageBean.getPageNumber(), pageBean.getPageSize());
        page = tbMessageMapper.selectPage(page, wrapper);
        //获取发帖的人员信息
        page.getRecords().forEach(message -> {
            message.setUser(authService.queryById(message.getUid()).view());
            message.setMine(message.getUid().equals(authInfo.getUid()));
        });

        return R.ok(page);
    }

    @Override
    public R<String> delete(Integer id) {
        AuthInfo authInfo = AppInterceptor.getAuthInfo();
        //校验帖子是否存在
        TbMessage tbmessage = tbMessageMapper.selectById(id);
        if (tbmessage == null) {
            return R.fail("帖子不存在", "");
        }
        //自己只能删除自己的帖子
        if (!tbmessage.getUid().equals(authInfo.getUid())) {
            return R.fail("只能删除自己的帖子", "");
        }
        int i = tbMessageMapper.deleteById(id);
        return i == 1 ? R.ok("删除成功", "") : R.fail("删除失败", "");
    }
    @Override
    public R<String> update(TbMessage tbMessage) {
        AuthInfo authInfo = AppInterceptor.getAuthInfo();
        //校验帖子是否存在
        TbMessage tbmessage = tbMessageMapper.selectById(tbMessage.getId());
        if (tbmessage == null) {
            return R.fail("帖子不存在", "");
        }
        //帖子只能更新自己
        if (!tbmessage.getUid().equals(authInfo.getUid())) {
            return R.fail("只能更新自己的帖子", "");
        }
        int i = tbMessageMapper.updateById(tbMessage);
        return i == 1 ? R.ok("更新成功", "") : R.fail("更新失败", "");
    }

    public R<String> queryBuId(Integer id) {
        return null;
    }
}
